﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.MyClub.Controllers
{
    [Authorize(Roles = "Users")]
    public class CompetitorsController : Controller
    {
        atletisEntities db = new atletisEntities();
        
        // GET: /MyClub/Competitors/
        public ActionResult Index()
        {
            var competitors = db.Competitors.Where(c => c.ClubId == User.Identity.Name).OrderBy(c => c.Surname).ToList();
            ViewBag.Clubs = db.Clubs.SingleOrDefault(c => c.ID == User.Identity.Name);
            return View(competitors);
        }

        // GET: /MyClub/CompetitorsCreate
        public ActionResult Create()
        {
            ViewBag.Clubs = db.Clubs.Single(c => c.ID == User.Identity.Name);

            var clubs = db.Clubs.Where(c => c.ID == User.Identity.Name).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", User.Identity.Name);

            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", "SVK");
            return View();
        }

        // POST: /MyClub/CompetitorsCreate
        [HttpPost]
        public ActionResult Create(Competitors competitors)
        {
            competitors.Active = false;
            competitors.Sync = true;

            if (ModelState.IsValid)
            {
                db.Competitors.AddObject(competitors);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ClubId = new SelectList(db.Clubs, "ID", "Name", competitors.ClubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", competitors.CountryId);
            return View(competitors);
        }

        // GET: /MyClub/CompetitorsEdit
        public ActionResult Edit(int id)
        {
            Competitors competitors = db.Competitors.Single(c => c.ID == id);
            ViewBag.Clubs = db.Clubs.Single(c => c.ID == competitors.ClubId);

            var clubs = db.Clubs.Where(c => c.ID == competitors.ClubId).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", competitors.ClubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", competitors.CountryId);
            return View(competitors);
        }

        // POST: /MyClub/CompetitorsEdit
        [HttpPost]
        public ActionResult Edit(Competitors competitors)
        {
            if (ModelState.IsValid)
            {
                db.Competitors.Attach(competitors);
                db.ObjectStateManager.ChangeObjectState(competitors, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ClubId = new SelectList(db.Clubs, "ID", "Name", competitors.ClubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", competitors.CountryId);
            return View(competitors);
        }





    }
}
